package com.guaoran.webmvc.controller;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : gucheng
 * @Description : <br/>
 *  异常处理
 * @date :2018/11/13 9:13
 */
@RestController
public class DemoRestController {
    @GetMapping("/index")
    public String index(){
        System.out.println("进入index中......");
        return "hello world";
    }
    /**
     * springboot处理页面找不到的情况
     * @param status
     * @param request
     * @param throwable
     * @return
     */
    @GetMapping("/404.html")
    public Map<String,Object> handlePageNotFound(HttpStatus status,
                                                 HttpServletRequest request,
                                                 Throwable throwable) {

        Map<String,Object> errors = new HashMap<>();

        errors.put("statusCode",
                request.getAttribute("javax.servlet.error.status_code"));
        errors.put("requestUri",
                request.getAttribute("javax.servlet.error.request_uri"));

        return errors;
    }

    /**
     * 采用MVC方式，对异常进行捕获
     * @return
     */
    @RequestMapping("/npe")
    public String npe(){
        throw new NullPointerException("故意抛异常！");
    }
    /**
     * 采用MVC方式，对异常进行捕获
     * @return
     */
    @RequestMapping("/ioobe")
    public String ioobe(){
        List list = new ArrayList();
        list.get(0);
        return null;
    }
}
